﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FWMS
{
    public partial class Form_changpinguanli : Form
    {
        public Form_changpinguanli()
        {
            InitializeComponent();
        }
        //数据库中数据的总行数
        private static int resultrows;
        //查询结果的数据
        public object[,] Selectresult;
        //条件查询结果的数据
        public object[,] ConditionSelectresult;
        //双击选择的产品名称
        private static string selectchanpinname;
        //页面初始化
        private void Form_changpinguanli_Load(object sender, EventArgs e)
        {
            this.tb_chanpinname.Focus();
            this.comboBox1.Items.Add("");
            this.comboBox1.Items.Add("成品");
            this.comboBox1.Items.Add("半成品");
            this.bt_chanpinmodify.Enabled = false;
            selectall();
        }
        //成品添加
        private void bt_chanpinadd_Click(object sender, EventArgs e)
        {
            Service.ChanPinGuanLiService chanpinguanliservice = new FWMS.Service.ChanPinGuanLiService();
            Bean.ChanPinGuanLiBean bean = new FWMS.Bean.ChanPinGuanLiBean();

            if ("".Equals(this.tb_chanpinname.Text.Trim()))
            {
                MessageBox.Show("成品名不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_chanpinname.Focus();

            }
            else if (comboBox1.SelectedItem == null || "".Equals((string)comboBox1.SelectedItem))
            {
                MessageBox.Show("成品类型不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.comboBox1.Focus();
                return;
            }
            else
            {
                bean.Chanpinname = this.tb_chanpinname.Text.Trim();
                bean.Chanpintype = (string)this.comboBox1.SelectedItem;
                if (Selectresult != null)
                {
                    if (this.chanpinsbianli(Selectresult, this.tb_chanpinname.Text.Trim(), Selectresult.Length / 2))
                    {
                        if (chanpinguanliservice.chanpinguanliServiceAdd(bean))
                        {
                            MessageBox.Show("成品添加成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            selectall();
                            foreach (DataGridViewRow row in this.dataGridView1.Rows)
                            {
                                if (bean.Chanpinname.Equals(row.Cells[1].Value))
                                {
                                    this.dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;

                                    row.DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                                    break;
                                }
                            }
                            this.tb_chanpinname.Focus();
                        }
                        else
                        {
                            MessageBox.Show("成品添加失败，请重新添加！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            this.tb_chanpinname.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show("成品添加失败，成品库中已存在该成品！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.tb_chanpinname.Focus();
                    }
                }
                else//第一次添加数据
                {
                    if (chanpinguanliservice.chanpinguanliServiceAdd(bean))
                    {
                        MessageBox.Show("成品添加成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        selectall();
                        foreach (DataGridViewRow row in this.dataGridView1.Rows)
                        {
                            if (bean.Chanpinname.Equals(row.Cells[1].Value))
                            {
                                this.dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;

                                row.DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                                break;
                            }
                        }
                        this.tb_chanpinname.Focus();
                    }
                    else
                    {
                        MessageBox.Show("成品添加失败，请重新添加！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.tb_chanpinname.Focus();
                    }
                }
            }

            this.bt_chanpinmodify.Enabled = false;
        }
        //接收数据并显示
        private void datagridviewadddata(Bean.ChanPinGuanLiBean bean)
        {
            this.dataGridView1.Rows.Clear();
            this.label_selectresult.Text = "查询结果：" + " " + bean.Resultrow.ToString();
            for (int j = 0; j < bean.Resultrow; j++)
            {
                this.dataGridView1.Rows.Add(bean.Selectresult[j, 0], bean.Selectresult[j, 1]);
            }
        }
        //查询全部数据
        private void selectall()
        {
            //查询全部SERVICE层
            Service.ChanPinGuanLiService chanpinguanliservice = new FWMS.Service.ChanPinGuanLiService();
            //产品管理BEAN
            Bean.ChanPinGuanLiBean bean = new FWMS.Bean.ChanPinGuanLiBean();

            bean = chanpinguanliservice.chanpinguanliServiceSelect();
            if (bean.Selectresult == null)
            {
                MessageBox.Show("数据库中无数据");
                this.label_selectresult.Text = "查询结果" + " " + "0";
                resultrows = 0;
                Selectresult = null;
                ConditionSelectresult = null;
            }
            else
            {
                resultrows = bean.Resultrow;//查询结果的总行数
                Selectresult = bean.Selectresult;
                ConditionSelectresult = bean.Selectresult;
                datagridviewadddata(bean);  //显示查询数据s
            }
        }
        //显示数据集双击事件
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            selectchanpinname = (string)ConditionSelectresult[this.dataGridView1.CurrentCell.RowIndex, 0].ToString().Trim();
            this.tb_chanpinname.Text = (string)ConditionSelectresult[this.dataGridView1.CurrentCell.RowIndex, 0];
            this.comboBox1.SelectedItem = (string)ConditionSelectresult[this.dataGridView1.CurrentCell.RowIndex, 1];
            this.bt_chanpinmodify.Enabled = true;
        }
        //条件查询
        private void bt_chanpinselect_Click(object sender, EventArgs e)
        {
            //任何条件都不加 查询全部
            if ("".Equals(this.tb_chanpinname.Text.Trim()) && (comboBox1.SelectedItem == null || "".Equals((string)comboBox1.SelectedItem)))
            {
                    selectall();
                    return;
            }
            else
            {
                Service.ChanPinGuanLiService chanpinguanliservice = new FWMS.Service.ChanPinGuanLiService();
                Bean.ChanPinGuanLiBean bean = new FWMS.Bean.ChanPinGuanLiBean();
                bean.Chanpinname = this.tb_chanpinname.Text.Trim();
                bean.Chanpintype = (string)this.comboBox1.SelectedItem;
                bean = chanpinguanliservice.chanpinguanliServiceSelectContation(bean);
                if (bean.Selectresult == null)
                {
                    MessageBox.Show("成品库中没有当前成品！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.tb_chanpinname.Focus();
                }
                else
                {
                    resultrows = bean.Resultrow;
                    ConditionSelectresult = bean.Selectresult;
                    datagridviewadddata(bean);
                }
            }

            this.bt_chanpinmodify.Enabled = false;
        }
        //产品修改
        private void bt_chanpinmodify_Click(object sender, EventArgs e)
        {
            if ("".Equals(this.tb_chanpinname.Text.Trim()))
            {
                MessageBox.Show("成品名不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_chanpinname.Focus();
                return;
            }
            else if (comboBox1.SelectedItem == null || "".Equals((string)comboBox1.SelectedItem))
            {
                MessageBox.Show("成品类型不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.comboBox1.Focus();
                return;
            }
            if (!selectchanpinname.Equals(this.tb_chanpinname.Text.Trim()))
            {
                if (this.chanpinsbianli(Selectresult, this.tb_chanpinname.Text.Trim(), Selectresult.Length/2))
                {
                    MessageBox.Show("成品修改失败，成品库中存在同名材料！", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.tb_chanpinname.Focus();
                    return;
                }
            }
            
            Service.ChanPinGuanLiService chanpinguanliservice = new FWMS.Service.ChanPinGuanLiService();
            Bean.ChanPinGuanLiBean bean = new FWMS.Bean.ChanPinGuanLiBean();

            bean.Chanpinname = this.tb_chanpinname.Text.Trim();     
            bean.Chanpintype = (string)this.comboBox1.SelectedItem;
            bean.Id = selectchanpinname;
            if (chanpinguanliservice.chanpinguanliServiceModify(bean))
            {
                selectall();
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    if (bean.Chanpinname.Equals(row.Cells[0].Value))
                    {
                        if (row.Index > 12)
                        {
                            this.dataGridView1.FirstDisplayedScrollingRowIndex = row.Index - 3;
                        }
                        row.DefaultCellStyle.BackColor = System.Drawing.Color.LightPink;
                        break;
                    }
                }
                MessageBox.Show("成品修改成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.tb_chanpinname.Focus();
            }
            else
            {
                MessageBox.Show("成品修改失败！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.tb_chanpinname.Focus();
            }
        }
        //遍历数组，查询数组中是否有相关的字符串
        private bool chanpinsbianli(object[,] Selectresult, string str, int rows)
        {
            string strcailiaoname;
            for (int i = 0; i < rows; i++)
            {
                strcailiaoname = (string)Selectresult[i, 0];
                if (str.Trim().Equals(strcailiaoname.Trim()))
                {
                    return false;
                }
            }
            return true;
        }
    }
}
